package edu.mse.camel.client.posterboard.ui.handlers;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;

import edu.mse.camel.client.core.session.event.PBInitiationEvent;
import edu.mse.camel.client.eventbus.pubsub.ISubscriber;
import edu.mse.camel.client.events.IEvent;
import edu.mse.camel.client.posterboard.posterboard.Posterboard;
import edu.mse.camel.client.posterboard.posterboard.PosterboardFactory;
import edu.mse.camel.client.posterboard.ui.util.SelectUtil;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardFactory;

public class PBInitiationEventHandler implements ISubscriber {

	@Override
	public void notifyEvent(IEvent e) {
		if (e instanceof PBInitiationEvent) {
			PBInitiationEvent pbie = (PBInitiationEvent) e;
			if (openPosterboard(pbie) && openWhiteBoard(pbie)) {
				pbie.setOpSuccessful(true);
			}			
		}
	}

	private boolean openWhiteBoard(final PBInitiationEvent pbie) {
		
		if (pbie.isWhiteboard()){

			final String whiteboardDiagramFile = pbie.getWhiteboardDiagramFile();

			/*
			 * If the user wants to create a new whiteboard, create the new resource 
			 */
			if(pbie.isNewWhiteboard()){
				
				WhiteboardDiagram wb = WhiteboardFactory.eINSTANCE.createWhiteboardDiagram();
				Resource res = new XMIResourceImpl(
						getURI(pbie.getProjectName(), whiteboardDiagramFile));
				res.getContents().add(wb);
	
				try {
					res.save(null);
					pbie.setWhiteboardDiagramFile(whiteboardDiagramFile);
					return true;
				} catch (IOException e1) {
					e1.printStackTrace();
					return false;
				}
			} else {
				pbie.setWhiteboardDiagramFile(whiteboardDiagramFile);
			}
			
		}

		return true;
	}

	private boolean openPosterboard(PBInitiationEvent pbie) {
		Posterboard pb = PosterboardFactory.eINSTANCE.createPosterboard();
		if (pbie.getModelName() != null
				&& pbie.getDiagramName() != null) {
			pb.setUmlFile(pbie.getModelName());
			pb.setDiagramFile(pbie.getDiagramName());
			
		}
		if (pbie.isWhiteboard()) {
			pb.setWhiteboardFile(pbie.getWhiteboardDiagramFile());				
		}
		
		URI resourceURI = getURI(pbie.getProjectName(), pbie.getPbFile());
		Resource res = new XMIResourceImpl(resourceURI);
		res.getContents().add(pb);
		
		try {
			res.save(null);
			pbie.setShouldOpenPosterBoard(true);
			
			//Select the posterboard in the navigator and all other views
			if (resourceURI.isPlatformResource()) {
				String platformString = resourceURI.toPlatformString(true);
				final IFile finalCopyOfIFile = 
					(IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);
				SelectUtil.selectAndReveal(finalCopyOfIFile);
			}
			
			return true;
		} catch (IOException e1) {
			e1.printStackTrace();
			return false;
		}
	}
	
	private URI getURI(String projectName, String fileName) {
		String pathName = "/" + projectName + "/" + fileName;
		return URI.createPlatformResourceURI(pathName, true);
	}
	
}
